package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.UrgentOrPostponeInfoVO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UrgentOrPostponeInfoModuleHandler extends AbstractModuleHandler {

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        UrgentOrPostponeInfoVO urgentOrPostponeInfoVO = new UrgentOrPostponeInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(context.getAcceptRequest().getSceneInstId(), "142");
        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            commonAttrList.removeIf(
                commonAttr -> !KiteStringUtils.equalsAny(commonAttr.getShowType(), KeyConsts.PROD_ATTR_SHOW_TYPE_D, KeyConsts.PROD_ATTR_SHOW_TYPE_M));
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            urgentOrPostponeInfoVO.setUrgentOrPostponeAttr(instAttrDTOList);
            SceneNewOfferSeneVO sceneInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneInfoVo.setUrgentOrPostponeInfoVO(urgentOrPostponeInfoVO);
        }
        return Collections.singletonList(urgentOrPostponeInfoVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        UrgentOrPostponeInfoVO urgentOrPostponeInfoVO = new UrgentOrPostponeInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(context.getAcceptRequest().getSceneInstId(), "142");
        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            //设置预览模式
            commonAttrList.forEach((attr) -> {
                attr.setAttrValueTypeId("TT");
            });
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            urgentOrPostponeInfoVO.setUrgentOrPostponeAttr(instAttrDTOList);
            SceneNewOfferSeneVO sceneInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneInfoVo.setUrgentOrPostponeInfoVO(urgentOrPostponeInfoVO);
        }
        return Collections.singletonList(urgentOrPostponeInfoVO);
    }
}

